#!/command/with-contenv bashio
# vim: ft=bash
# shellcheck shell=bash
# ==============================================================================
# Start Whisper service
# ==============================================================================
flags=()

if [ "$(uname -m)" == "x86_64" ] && ! grep -qw 'avx' /proc/cpuinfo; then
    bashio::log.warning "Your CPU does not support Advanced Vector Extensions (AVX). Whisper will run slower than normal."
fi

model="$(bashio::config 'model')"
if [ "${model}" = 'custom' ]; then
    # Override with custom model
    model="$(bashio::config 'custom_model')"
    if [ -z "${model}" ]; then
      bashio::exit.nok "Custom model is not set"
    fi
    custom_model_type="$(bashio::config 'custom_model_type')"
    if [ "${custom_model_type}" = 'transformers' ]; then
      flags+=('--use-transformers')
    fi
fi

if bashio::config.true 'debug_logging'; then
    flags+=('--debug')
fi

# shellcheck disable=SC2068
exec python3 -m wyoming_faster_whisper \
    --uri 'tcp://0.0.0.0:10300' \
    --model "${model}" \
    --beam-size "$(bashio::config 'beam_size')" \
    --language "$(bashio::config 'language')" \
    --initial-prompt "$(bashio::config 'initial_prompt')" \
    --data-dir /data \
    --download-dir /data ${flags[@]}
